--[[
Harvest - collects the names of Blizzard's global WoW/Lua API functions

version: 1.2.1
author: Tabithy
email: tabithy@gmail.com
website: http://www.googlecode.com/p/tabstools

based on the script posted here: 
http://www.wowwiki.com/User:Mikk/Scripts#Create_Global_Function_Lis
]]

local global = set{}

-- Harvested functions that should be ignored
local ignored = set{'Harvest_OnEvent'}

-- Lua 5.1 functions in _G
local lua51 = set{'xpcall', 'tostring', 'print', 'unpack', 'require',
    'getfenv', 'setmetatable', 'next', 'assert', 'tonumber', 'rawequal',
    'collectgarbage', 'getmetatable', 'module', 'rawset', 'ipairs', 'pcall',
    'newproxy', 'type', 'select', 'gcinfo', 'pairs', 'rawget', 'loadstring',
    'dofile', 'setfenv', 'load', 'error', 'loadfile'}

-- Functions that can only be called from secure code
local protected = set{'AssistUnit', 'AttackTarget',
   'CameraOrSelectOrMoveStart', 'CameraOrSelectOrMoveStop',
   'CastShapeshiftForm', 'CastSpell', 'CastSpellByName',
   'ClearTarget', 'MoveBackwardStart', 'MoveBackwardStop',
   'MoveForwardStart', 'MoveForwardStop', 'RegisterForSave',
   'SpellStopCasting', 'SpellStopTargeting', 'SpellTargetUnit',
   'StartDuel', 'StrafeLeftStart', 'StrafeLeftStop',
   'StrafeRightStart', 'StrafeRightStop', 'TargetLastEnemy',
   'TargetLastTarget', 'TargetNearestEnemy', 'TargetNearestFriend',
   'TargetUnit', 'ToggleAutoRun', 'ToggleRun', 'TurnLeftStart',
   'TurnLeftStop', 'TurnOrActionStart', 'TurnOrActionStop',
   'TurnRightStart', 'TurnRightStop', 'UseAction', 'DeleteGMTicket',
   'GMRequestPlayerInfo', 'GetGMStatus', 'GetGMTicket', 
   'GetGMTicketCategories', 'GMSurveyAnswerSubmit', 'GMSurveyCommentSubmit',
   'GMSurveyQuestion', 'GMSurveySubmit', 'NewGMTicket', 'Stuck', 
   'UpdateGMTicket'}


local clientVersion, clientBuild = GetBuildInfo()


local function harvest()
    for i, v in pairs(_G) do
        if (type(v) == 'function') then
            set.m_add(global, i)
        end
    end
    DEFAULT_CHAT_FRAME:AddMessage("Harvest: WoW Client v" .. clientVersion .. 
            " (" .. clientBuild .. ")")
    DEFAULT_CHAT_FRAME:AddMessage("Harvest: found " .. set.len(global).. 
            " functions (" .. set.len(protected) .. " protected)")
end


local function clean()
    global = global - (ignored + protected + lua51)
end


local function makeSyntax()
    return
[[{
  scopeName = 'source.wow.lua';
  comment = 'WoW client v]] .. clientVersion .. [[ (]] 
      .. clientBuild .. [[); Based on the Lua bundle syntax v0.8 by Wes Houghton';
  fileTypes = ( 'lua' );
  foldingStartMarker = '^\s*\b(function|if|for)\b|{[ \t]*$|\[\[';
  foldingStopMarker = '\bend\b|^\s*}|\]\]';
  patterns = (
    { include = 'source.lua'; },
    { name = 'support.function.wow';
      match = '(?<![^.]\.|:)\b(]]
          .. set.concat(global, '|') .. [[)\b(?=[( {])';
    },
    { name = 'support.function.wow.protected';
      match = '(?<![^.]\.|:)\b(]]
          .. set.concat(protected, '|') ..  [[)\b(?=[( {])';
    }
  );
}]]
end


local function handleSlash()
    harvest()
    clean()
    SYNTAX = makeSyntax()
end


local function init()
    SLASH_HARVEST1 = "/harvest"
    SlashCmdList["HARVEST"] = handleSlash  
end 


function Harvest_OnEvent()
    if (event == "VARIABLES_LOADED") then
        init()
    end
end
